package com.soladhoc.annonces.client.view.ui;

import static com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy.DISABLED;

import java.util.List;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.soladhoc.annonces.client.event.AnnonceAction;
import com.soladhoc.annonces.client.event.AnnonceActionEvent;
import com.soladhoc.annonces.client.event.AnnonceActionEvent.AnnonceActionHandler;
import com.soladhoc.annonces.client.ui.FormatUtils;
import com.soladhoc.annonces.server.domain.Annonce;
import com.soladhoc.annonces.shared.proxy.AnnonceKeyProvider;
import com.soladhoc.annonces.shared.proxy.AnnonceProxy;

public class AnnonceCellTable extends CellTable<AnnonceProxy> implements AnnonceActionEvent.HasAnnonceActionHandlers
{
    // -------------------------------------------------------- private members

    private final AnnonceCellTableResources resources;


    // ----------------------------------------------------------- constructors

    public AnnonceCellTable(final AnnonceCellTableResources resources)
    {
        super(Integer.MAX_VALUE, resources, new AnnonceKeyProvider());
        this.resources = resources;

        setRowCount(0);
        setKeyboardSelectionPolicy(DISABLED);
        addColumns();
    }


    // -------------------------------------------------------------- gui setup

    private void addColumns()
    {
        // Column #0: Name
        AnnonceColumn titleColumn = new AnnonceColumn(this, new AnnonceTextRenderer()
        {
            @Override
            protected String getValue(AnnonceProxy annonce)
            {
                return annonce.getTitle();
            }
        });
        addColumnStyleName(0, resources.cellTableStyle().titleColumn());
        addColumn(titleColumn, "Title");

        // Column #1: Description
        AnnonceColumn descriptionColumn = new AnnonceColumn(this, new AnnonceTextRenderer()
        {
            @Override
            protected String getValue(AnnonceProxy annonce)
            {
                return annonce.getTitle();
            }
        });
        addColumnStyleName(1, resources.cellTableStyle().descriptionColumn());
        addColumn(descriptionColumn, "Description");

        // Column #2: Due Date
        AnnonceColumn dueDateColumn = new AnnonceColumn(this, new AnnonceTextRenderer()
        {
            @Override
            public String getValue(AnnonceProxy annonce)
            {
                return FormatUtils.date(annonce.getCreationDate());
            }
        });
        addColumnStyleName(2, resources.cellTableStyle().creationDateColumn());
        addColumn(dueDateColumn, "Due Date");

        // Column #3: Finished
        AnnonceColumn publishedColumn = new AnnonceColumn(this, new AnnonceRenderer()
        {
            @Override
            public SafeHtml render(AnnonceProxy annonce)
            {
                String entity = annonce.getPublished() ? "&#9745;" : "&#9744;";
                return new SafeHtmlBuilder().appendHtmlConstant(entity).toSafeHtml();
            }
        });
        addColumnStyleName(3, resources.cellTableStyle().publishedColumn());
        addColumn(publishedColumn, "Published");
    }


    public void update(List<AnnonceProxy> annonces)
    {
        if (annonces != null)
        {
            setRowData(0, annonces);
            setRowCount(annonces.size());
        }
    }


    // --------------------------------------------------------- event handling

    @Override
    public HandlerRegistration addAnnonceActionHandler(AnnonceActionHandler handler)
    {
        return addHandler(handler, AnnonceActionEvent.getType());
    }


    void onDetail(AnnonceProxy annonce)
    {
        AnnonceActionEvent.fire(this, annonce, AnnonceAction.Action.DETAIL);
    }
}
